<?php
namespace Apipad\Controller;

use Think\Controller;

//仓库
class UserKuerpWarehouseController extends UserWarehouseController
{


    //搜索条件前置执行
    public function _before_search() {
        $map = array();
        $map['is_delete'] = 0;
        return $map;
    }

    //搜索条件前置执行
    public function _before_add() {
        if(IS_POST){
            $kw_model = M('UserKuerpWarehouse');
            //10=普通仓库 1=款式，2=主辅料 ，3=订单仓
            $_POST['code']= $this->getsn('CKBH');
            if($_POST['name']=="undefined" || empty($_POST['name'])){
                $this->ajaxErrReturn('请输入仓库名称');
            }
            $atype = $_POST['atype'];
            $_POST['pic'] = I('pic');
            if($atype==0 && $pic=="" || $atype==0 && $pic=='undefined'){
                $_POST['pic']='https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/04/10/6433ba9fac756.jpg';
            }
            if($atype==1 && $pic=="" || $atype==1 && $pic=='undefined'){
                $_POST['pic']='https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/04/10/6433b7b7ac7a7.jpg';
            }
            if($atype==2 && $pic=="" || $atype==2 && $pic=='undefined'){
                $_POST['pic']='https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/04/10/6433bad2adfec.jpg';
            }
            if($atype==3 && $pic=="" || $atype==3 && $pic=='undefined'){
                $_POST['pic']='https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/04/10/6433bb0c52c08.jpg';
            }
 
        }
    }

    //添加后置
    function _after_add($id=0){
        if(IS_POST && I('loc_names') && $id){
            $loc_names = str_ireplace('，',',',I('loc_names'));
            $loc_name_ar = explode(',',$loc_names);
            //直接添加库位
            $kwl_model = D('UserKuerpWarehouseLocation');
            //dump($loc_name_ar);exit;
            foreach($loc_name_ar as $loc_name){
                $kwl_data['wh_id'] = $id;
                $kwl_data['uid'] = UID;
                $kwl_data['loc_name'] = $loc_name;
                $kwl_data['add_time'] = NOW_TIME;
                $kwl_data['edit_time'] = NOW_TIME;
                $res = $kwl_model->add($kwl_data);
                if(!$res){
                    echo M()->_sql();exit;
                }

            }
            $kw_model = D('UserKuerpWarehouse');
            $kw_model->tjkw($id);
        }
        //dump($id);exit;
    }

    //删除
    function vdel(){
        $lp_model = M('UserKuerpWarehouseLocProduct');
        $lp_wdata['wh_id'] = I('id');
        $lp_stock = $lp_model->where($lp_wdata)->sum('stock');
        if($lp_stock>0){
            $this->ajaxErrReturn('仓库有库存不能删除');
        }
        $kw_model = M('UserKuerpWarehouse');
        $kw_wdata['uid'] = UID;
        $kw_wdata['id'] = I('id');
        $kw_sdata['status'] = 0;
        $kw_sdata['is_delete'] = 1;
        $res = $kw_model->where($kw_wdata)->save($kw_sdata);
        if(!$res){
            $this->ajaxErrReturn('删除失败');
        }
        $kwl_model = M('UserKuerpWarehouseLocation');
        $kwl_wdata['uid'] = UID;
        $kwl_wdata['wh_id'] = I('id');
        $kw_sdata['is_delete'] = 1;
        $kwl_info = $kwl_model->where($kwl_wdata)->find();
        if($kwl_info){
            $res_kwl = $kw_model->where($kw_wdata)->save($kw_sdata);
            if(!$res_kwl){
                $this->ajaxErrReturn('删除失败2');
            }
        }
        //echo M()->_sql();exit;
        $this->ajaxSucReturn('删除成功');
    }

    //仓库展示
    function show(){
        $model = M(CONTROLLER_NAME);
        $wdata['id'] = I('id');
        $ck_vo = $model->where($wdata)->find();
        $this->assign('ck_vo',$ck_vo);
        $cat_model = M('Imgcate');
        $kwlp_model = D('UserKuerpWarehouseLocProduct');
        if($ck_vo['atype']==1){
            //成品仓分类
            $c_list = $cat_model->where(['status' => 3, 'pid' => 0])->order("id asc")->select();
            if ($c_list) {
                foreach ($c_list as $k => $val) {
                    $list2 = $cat_model->where(['status' => 3, 'pid' => $val['id']])->order("id asc")->select();
                    if ($list2) {
                        $c_list[$k]['child'] = $list2;
                        foreach ($list2 as $k1 => $val2) {
                            $list3 = $cat_model->where(['status' => 3, 'pid' => $val2['id']])->order("id asc")->select();
                            if ($list3) {
                                $c_list[$k]['child'][$k1]['child2'] = $list3;
                            }
                        }
                    }
                }
            }
            //仓库商品列表
            $p_list = $this->getProduct($ck_vo['atype']);
            $this->assign('p_list',$p_list);
        }else if($ck_vo['atype']==2){
            //辅料仓
            $c_list = $cat_model->where(['status' => 2, 'pid' => 0])->select();
            //仓库商品列表
            $p_list = $this->getProduct($ck_vo['atype']);
            $this->assign('p_list',$p_list);

        }else if($ck_vo['atype']==3){
            //订单仓
            $c_list = $cat_model->where(['status' => 3, 'pid' => 0])->order("id asc")->select();
            if ($c_list) {
                foreach ($c_list as $k => $val) {
                    $list2 = $cat_model->where(['status' => 3, 'pid' => $val['id']])->order("id asc")->select();
                    if ($list2) {
                        $c_list[$k]['child'] = $list2;
                        foreach ($list2 as $k1 => $val2) {
                            $list3 = $cat_model->where(['status' => 3, 'pid' => $val2['id']])->order("id asc")->select();
                            if ($list3) {
                                $c_list[$k]['child'][$k1]['child2'] = $list3;
                            }
                        }
                    }
                }
            }
            //仓库商品列表
            $p_list = $this->getProduct($ck_vo['atype']);
            $this->assign('p_list',$p_list);
        }else{
            //其他仓
            //成品仓分类
            $c_list = $cat_model->where(['status' => 10, 'pid' => 0])->order("id asc")->select();
            if ($c_list) {
                foreach ($c_list as $k => $val) {
                    $list2 = $cat_model->where(['status' => 10, 'pid' => $val['id']])->order("id asc")->select();
                    if ($list2) {
                        $c_list[$k]['child'] = $list2;
                        foreach ($list2 as $k1 => $val2) {
                            $list3 = $cat_model->where(['status' => 10, 'pid' => $val2['id']])->order("id asc")->select();
                            if ($list3) {
                                $c_list[$k]['child'][$k1]['child2'] = $list3;
                            }
                        }
                    }
                }
            }
            //仓库商品列表
            $p_list = $this->getProduct($ck_vo['atype']);
            $this->assign('p_list',$p_list);
        }
        $data['c_list'] = $c_list;
        $data['p_list'] = $p_list;
        $this->ajaxSucReturn($data);
        //$this->assign('c_list',$c_list);
        //$this->whDisplay();
    }

    //按仓库类型展示
    function stockList(){
        $atype = I('atype') ? : 1;
        $page = $post['page'] ? : 1;
        $limit = $post['limit'] ? : 20; 
        //仓库列表
        $kw_model = M('UserKuerpWarehouse');
        $kw_wdata['status'] = 1;
        $kw_wdata['atype'] = $atype;
        $list = $kw_model->where($kw_wdata)->select();
        if($list){
            foreach($list as $val){
                $ids[] = $val['id'];
            }
            $_POST['id'] = implode(',',$ids);
            $p_list = $this->getProduct($atype);
        }
        $cat_model = M('Imgcate');
        if($atype==1  || $atype==3 ){
            //成品仓分类
            /*
            $c_list = $cat_model->where(['status' => 3, 'pid' => 0,'is_delete'=>0])->order("id asc")->select();
            if ($c_list) {
                foreach ($c_list as $k => $val) {
                    $list2 = $cat_model->where(['status' => 3, 'pid' => $val['id'],'is_delete'=>0])->field('id,title,pid')->order("id asc")->select();
                    if ($list2) {
                        $c_list[$k]['child'] = $list2;
                        foreach ($list2 as $k1 => $val2) {
                            $list3 = $cat_model->where(['status' => 3, 'pid' => $val2['id'],'is_delete'=>0])->field('id,title,pid')->order("id asc")->select();
                            if ($list3) {
                                $c_list[$k]['child'][$k1]['child'] = $list3;
                            }
                        }
                    }
                }
            }
            */
            $model = D('Imgcate');
            $map['pid'] = 0;
            $map['status'] = 3;
            $map['is_delete'] = 0;
            $data = $model->getList($map, 10000, 'pid asc,id asc');
            $c_list = $data['list'];
        }else if($atype==2){
            //$c_list = $cat_model->where(['status' => 2, 'pid' => 0,'is_delete'=>0])->select();
            $model = D('Imgcate');
            $map['pid'] = 0;
            $map['status'] = 2;
            $map['is_delete'] = 0;
            $data = $model->getList($map, 10000, 'pid asc,id asc');
            $c_list = $data['list'];
        }else if($atype==3){
            //订单仓
            /*
            $c_list = $cat_model->where(['status' => 3, 'pid' => 0,'is_delete'=>0])->order("id asc")->select();
            if ($c_list) {
                foreach ($c_list as $k => $val) {
                    $list2 = $cat_model->where(['status' => 2, 'pid' => $val['id'],'is_delete'=>0])->field('id,title,pid')->order("id asc")->select();
                    if ($list2) {
                        $c_list[$k]['child'] = $list2;
                        foreach ($list2 as $k1 => $val2) {
                            $list3 = $cat_model->where(['status' => 2, 'pid' => $val2['id'],'is_delete'=>0])->field('id,title,pid')->order("id asc")->select();
                            if ($list3) {
                                $c_list[$k]['child'][$k1]['child'] = $list3;
                            }
                        }
                    }
                }
            }
            */
            $model = D('Imgcate');
            $map['pid'] = 0;
            $map['status'] = 3;
            $map['is_delete'] = 0;
            $data = $model->getList($map, 10000, 'pid asc,id asc');
            $c_list = $data['list'];
        }else{
            //其他仓
            //成品仓分类
            /*
            $c_list = $cat_model->where(['status' => 10, 'pid' => 0,'is_delete'=>0])->order("id asc")->select();
            if ($c_list) {
                foreach ($c_list as $k => $val) {
                    $list2 = $cat_model->where(['status' => 10, 'pid' => $val['id'],'is_delete'=>0])->order("id asc")->select();
                    if ($list2) {
                        $c_list[$k]['child'] = $list2;
                        foreach ($list2 as $k1 => $val2) {
                            $list3 = $cat_model->where(['status' => 10, 'pid' => $val2['id'],'is_delete'=>0])->order("id asc")->select();
                            if ($list3) {
                                $c_list[$k]['child'][$k1]['child2'] = $list3;
                            }
                        }
                    }
                }
            }
            */
            $model = D('Imgcate');
            $map['pid'] = 0;
            $map['status'] = 10;
            $map['is_delete'] = 0;
            $data = $model->getList($map, 10000, 'pid asc,id asc');
            $c_list = $data['list'];
        }
        $data['c_list'] = $c_list;
        $data['p_list'] = $p_list;
        $this->ajaxSucReturn($data);
    }

    //根据不同仓库类型获得商品
    function getProduct($atype){
        $def_pic = './Public/Theme1/img/default_2.jpg';
        $kwlp_model = D('KuerpWarehouseLocProduct');
        $listRows = 20;
        if($atype==1){
            //成品仓
            //仓库商品列表
            $kwp_model = D('UserKuerpWarehouseProduct');
            $kwp_wdata['wh_id'] = array('in',I('id'));
            $kwp_wdata['kwp.atype'] = 1;
            if(I('cat_id')){
                $fsdad=$this->get_child_cc(I('cat_id'));
                $kwp_wdata['dl.cate_id'] = ['in',$fsdad];
            }
            if(I('keyword')){
               $kwp_wdata['dl.title|dl.goods_sn'] = array('like','%'.I('keyword').'%');
            }
            $subQuery = $kwp_model->alias('kwp')->join('__DESIGEN_LIST__ as dl on kwp.product_id=dl.id','left')->field('kwp.id')->group('product_id')->where($kwp_wdata)->buildSql();
            $count = $kwp_model->table($subQuery.' a')->count();
            $p = new \Think\AdminPage($count, $listRows);

            $p_list = $kwp_model->alias('kwp')->join('__DESIGEN_LIST__ as dl on kwp.product_id=dl.id ','left')->field('kwp.*,sum(stock) as stock,dl.title,dl.picc,dl.warn_number as p_warn_number,dl.chenfen,dl.goods_sn,dl.goods_attr,dl.price')->group('product_id')->where($kwp_wdata)->limit($p->firstRow . ',' . $p->listRows)->select();
            $dg_model = M('DesigenGuigedetail');
            foreach($p_list as &$val){
                if($val['warn_number']===null){
                    $val['warn_number'] = $val['p_warn_number'];
                }
                $val['stock'] = floatval($val['stock']);
                if($val['warn_number']>0 && $val['warn_number']>$val['stock']){
                    $val['stock'] = '<font color="red">'.$val['stock'].'</font>';
                }
                //规格数量
                $val['product_name'] = $val['title'];
                $dg_wdata['atype'] = 1;
                $dg_wdata['design_id'] = $val['product_id'];
                $spec_num = $dg_model->where($dg_wdata)->count() ? : 0;
                $val['spec_num'] = $spec_num;
                //库位数
                $kwlp_wdata['wh_id'] = $val['wh_id'];
                $kwlp_wdata['atype'] = 10;
                $kwlp_wdata['product_id'] = $val['product_id'];
                $kwlp_wdata['item_id'] = $val['item_id'];
                $loc_count = $kwlp_model->where($kwlp_wdata)->count();
                $val['loc_count'] = $spec_num;
                $pics = explode(',',$val['picc']);
                $val['pics'] = $pics;
                $val['pic'] = $pics[0] ? : $def_pic;
            }
        }else if($atype==2){
            //辅料仓
            //仓库商品列表
            $kwp_model = D('UserKuerpWarehouseProduct');
            //$kwp_wdata['wh_id'] = I('id');
            $kwp_wdata['wh_id'] = array('in',I('id'));
            $kwp_wdata['kwp.atype'] = 2;
            if(I('cat_id')){
                $fsdad=$this->get_child_cc(I('cat_id'));
                $kwp_wdata['dl.pid'] = ['in',$fsdad];
            }
            if(I('keyword')){
               $kwp_wdata['dl.title|dl.goods_sn'] = array('like','%'.I('keyword').'%');
            }
            $subQuery = $kwp_model->alias('kwp')->join('__IMG__ as dl on kwp.product_id=dl.id','left')->field('kwp.id')->group('product_id')->where($kwp_wdata)->buildSql();
            $count = $kwp_model->table($subQuery.' a')->count();
            $p = new \Think\AdminPage($count, $listRows);

            $p_list = $kwp_model->alias('kwp')->join('__IMG__ as dl on kwp.product_id=dl.id ','left')->field('kwp.*,sum(stock) as stock,dl.title,dl.img,dl.warn_number as p_warn_number,dl.chenfen,dl.goods_sn,dl.goods_attr,dl.price')->group('product_id')->where($kwp_wdata)->limit($p->firstRow . ',' . $p->listRows)->select();
            $dg_model = M('DesigenGuigedetail');
            //dump($p_list);exit;
            $pof_model = D('ProductionOrderFuliao');
            foreach($p_list as &$val){
                //商品信息
                /*
                if(!$val['pic'] && $val['picc']){
                    $pics = explode(',',$val['picc']);
                    $val['pic'] = $pics[0];
                }
                */
                if($val['warn_number']===null){
                    $val['warn_number'] = $val['p_warn_number'];
                }
                /*
                if($val['guige']){
                    $spec = $val['guige'].' '.$val['color'];
                    $val['spec'] = $spec;
                }
                */
                $val['stock'] = floatval($val['stock']);
                if($val['warn_number']>0 && $val['warn_number']>$val['stock']){
                    $val['stock'] = '<font color="red">'.$val['stock'].'</font>';
                }
                $val['product_name'] = $val['title'];
                $val['pic'] = $val['img'] ? : $def_pic;
                //$val['color_name'] = $val['color'];
                //规格数量                
                $dg_wdata['atype'] = 2;
                $dg_wdata['design_id'] = $val['product_id'];
                $spec_num = $dg_model->where($dg_wdata)->count() ? : 0;
                $val['spec_num'] = $spec_num;
                //库位数
                $kwlp_wdata['wh_id'] = $val['wh_id'];
                $kwlp_wdata['atype'] = 10;
                $kwlp_wdata['product_id'] = $val['product_id'];
                $kwlp_wdata['item_id'] = $val['item_id'];
                $loc_count = $kwlp_model->where($kwlp_wdata)->count();
                $val['loc_count'] = $spec_num;
                //占用情况
                //$pof_wdata['pof.product_id'] = $val['product_id'];
                //$pof_wdata['po.order_status'] = 1;
                //$employ_num = $pof_model->alias('pof')->join('__PRODUCTION_ORDER__ as po on pof.order_id=po.id','LEFT')->sum('order_num') ? : 0 ;
                $employ_num = $pof_model->getProductEmploy($val['product_id']);
                $val['employ_num'] = $employ_num;
            }

        }else if($atype==3){
            //订单仓
            //仓库商品列表
            $kwp_model = D('UserKuerpWarehouseOrder');
            $kwpl_model = D('UserKuerpWarehouseOrderLoc');
            $kwp_wdata['wh_id'] = array('in',I('id'));
            if(I('cat_id')){
                $fsdad=$this->get_child_cc(I('cat_id'));
                $kwp_wdata['dl.cate_id'] = ['in',$fsdad];
            }
            if(I('keyword')){
               $kwp_wdata['dl.title|dl.goods_sn'] = array('like','%'.I('keyword').'%');
            }
            //$p_list = $kwp_model->alias('kwp')->join('__DESIGEN_LIST__ as dl on kwp.sheji_ksid=dl.id ','left')->field('kwp.*,dl.title,dl.picc,dl.warn_number as p_warn_number,dl.chenfen,dl.goods_sn,dl.goods_attr')->where($kwp_wdata)->select();
            $subQuery = $kwp_model->alias('kwp')->join('__DESIGEN_LIST__ as dl on kwp.sheji_ksid=dl.id','left')->field('kwp.id')->group('sheji_ksid')->where($kwp_wdata)->buildSql();
            $count = $kwp_model->table($subQuery.' a')->count();
            $p = new \Think\AdminPage($count, $listRows);

            $p_list = $kwp_model->alias('kwp')->join('__DESIGEN_LIST__ as dl on kwp.sheji_ksid=dl.id ','left')->field('kwp.*,sum(stock) as stock,dl.title,dl.picc,dl.warn_number as p_warn_number,dl.chenfen,dl.goods_sn,dl.goods_attr,dl.price')->group('sheji_ksid')->where($kwp_wdata)->limit($p->firstRow . ',' . $p->listRows)->select();

            //读取
            //$fl_model = M('FuliaoList');

            $dg_model = M('DesigenGuigedetail');
            foreach($p_list as &$val){
                //商品信息
                if(!$val['pic'] && $val['picc']){
                    $pics = explode(',',$val['picc']);
                    $val['pic'] = $pics[0];
                }
                if($val['warn_number']===null){
                    //dump($val);exit;
                    $val['warn_number'] = $val['p_warn_number'];
                }
                if($val['guige']){
                    $spec = $val['guige'].' '.$val['color'];
                    $val['spec'] = $spec;
                }
                $val['stock'] = floatval($val['stock']);
                if($val['warn_number']>0 && $val['warn_number']>$val['stock']){
                    $val['stock'] = '<font color="red">'.$val['stock'].'</font>';
                }
                $val['product_name'] = $val['title'];
                $val['product_id'] = $val['sheji_ksid'];
                $val['atype'] = 2;
                //规格数量                
                $dg_wdata['atype'] = 1;
                $dg_wdata['design_id'] = $val['sheji_ksid'];
                $spec_num = $dg_model->where($dg_wdata)->count() ? : 0;
                $val['spec_num'] = $spec_num;
            }
        }else{
            //普通仓
            //仓库商品列表
            $kwp_model = D('UserKuerpWarehouseProduct');
            //$kwp_wdata['wh_id'] = I('id');
            $kwp_wdata['wh_id'] = array('in',I('id'));
            $kwp_wdata['kwp.atype'] = 10;
            if(I('cat_id'))$kwp_wdata['dl.cate_id'] = I('cat_id');
            if(I('keyword')){
               $kwp_wdata['dl.title|dl.goods_sn'] = array('like','%'.I('keyword').'%');
            }
            $subQuery = $kwp_model->alias('kwp')->join('__KUERP_OTHER_PRODUCT__ as dl on kwp.product_id=dl.id ','left')->field('kwp.id')->group('product_id')->where($kwp_wdata)->buildSql();
            $count = $kwp_model->table($subQuery.' a')->count();
            $p = new \Think\AdminPage($count, $listRows);
            $p_list = $kwp_model->alias('kwp')->join('__KUERP_OTHER_PRODUCT__ as dl on kwp.product_id=dl.id ','left')->field('kwp.*,sum(stock) as stock,dl.title,dl.pic,dl.warn_number as p_warn_number,dl.chenfen,dl.goods_sn,dl.goods_attr,dl.price')->group('product_id')->limit($p->firstRow . ',' . $p->listRows)->where($kwp_wdata)->select();
            $dg_model = M('DesigenGuigedetail');
            foreach($p_list as &$val){
                //商品信息
                if($val['warn_number']===null){
                    $val['warn_number'] = $val['p_warn_number'];
                }

                $val['stock'] = floatval($val['stock']);
                if($val['warn_number']>0 && $val['warn_number']>$val['stock']){
                    $val['stock'] = '<font color="red">'.$val['stock'].'</font>';
                }
                //规格数量
                $val['product_name'] = $val['title'];
                $dg_wdata['atype'] = 10;
                $dg_wdata['design_id'] = $val['product_id'];
                $spec_num = $dg_model->where($dg_wdata)->count() ? : 0;
                $val['spec_num'] = $spec_num;
                //库位数
                $kwlp_wdata['wh_id'] = $val['wh_id'];
                $kwlp_wdata['atype'] = 10;
                $kwlp_wdata['product_id'] = $val['product_id'];
                $kwlp_wdata['item_id'] = $val['item_id'];
                $loc_count = $kwlp_model->where($kwlp_wdata)->count();
                $val['loc_count'] = $spec_num;
            }
        }
        $data['count']=$count;
        $data['list']=$p_list;
        return $data;
    }
    //查询三级分类
    function get_child_cc($cat_id){
            $dra[]=$cat_id;
            //查询子分类
            $i_model = M('Imgcate');
            $spec_list = $i_model->field('id')->where(['pid'=>$cat_id])->select();
            if($spec_list){
              foreach($spec_list as $k=>$val){
                 $dra[]=$val['id'];
                 //查询三级分类
                 $cat_id2=$val['id'];
                 $spec_list2 = $i_model->field('id')->where(['pid'=>$cat_id2])->select();
                 if($spec_list2){
                    foreach($spec_list2 as $k1=>$vals){
                        $dra[]=$vals['id'];
                    }
                 }
              }
            }
           return $dra;

    }
    
    //规格列表(单仓库)
    function spec_lists(){
        $atype = I('atype');
        $id = I('product_id');
        //商品信息
        $d_model = M("UserGoods");
        $d_wdata['id'] = $id;
        $d_wdata['uid'] = UID;
        $vo = $d_model->where($d_wdata)->find();
        if(!$vo){
            $this->ajaxErrReturn('商品不存在'); 
        }
        //$product_name = $vo['title'];
        $kwlp_model = D('UserKuerpWarehouseLocProduct');
        if(I('wh_id'))$kwlp_wdata['wh_id'] = I('wh_id');
        $kwlp_wdata['product_id'] = $id;
        $dg_list = $kwlp_model->where($kwlp_wdata)->order('item_id asc')->select();
        $total_amount = 0;
        $total_stock = 0;
        $da_model = M("UserGoodsAttr");
        foreach($dg_list as &$dg_vo){
            $dg_vo['goods_sn'] = $vo['goods_sn'];
            $dg_vo['goods_attr'] = $vo['goods_attr'];
            $dg_vo['chenfen'] = $vo['chenfen'];
            $dg_vo['goods_sn'] = $vo['goods_sn'];
            $dg_vo['unit'] = $vo['unit'];
            $total_stock += $dg_vo['stock'];
            $da_wdata['id'] = $dg_vo['item_id'];
            $price = $da_model->where($da_wdata)->getField('price') ? : $vo['suppler_price'];
            $total_amount += $dg_vo['total_amount'] = $price*$dg_vo['stock'];
        }
        $vo['total_stock'] = $total_stock;
        $vo['total_amount'] = $total_amount;
        $data['product'] = $vo;
        $data['list'] = $dg_list;
        $this->ajaxSucReturn($data);
    }

    //规格列表(所有仓库)
    function stock_spec_lists(){
        $id = I('product_id');
        //商品信息
        $d_model = M("UserGoods");
        $d_wdata['id'] = $id;
        $d_wdata['uid'] = UID;
        $vo = $d_model->where($d_wdata)->find();
        if(!$vo){
            $this->ajaxErrReturn('商品不存在'); 
        }
        $kwlp_model = D('UserKuerpWarehouseLocProduct');
        $kwlp_wdata['atype'] = $atype;
        $kwlp_wdata['product_id'] = $id;
        $dg_list = $kwlp_model->where($kwlp_wdata)->order('item_id asc')->select();
        //dump($dg_list);exit;
        $total_amount = 0;
        $total_stock = 0;
        foreach($dg_list as &$dg_vo){
            $total_stock += $dg_vo['stock'];
            //$dg_vo['product_name'] = $dg_vo['product']['product_name'];
            $dg_vo['goods_sn'] = $dg_vo['product']['goods_sn'];
            $dg_vo['goods_attr'] = $dg_vo['product']['goods_attr'];
            $dg_vo['chenfen'] = $dg_vo['product']['chenfen'];
            $dg_vo['price'] = $dg_vo['product']['price'] ? : 0;
            $dg_vo['goods_sn'] = $dg_vo['product']['goods_sn'];
            $dg_vo['color_name'] = $dg_vo['product']['color'];
            $dg_vo['guige'] = $dg_vo['product']['guige'];
            $dg_vo['unit'] = $dg_vo['product']['unit'];
            $dg_vo['remark'] = $dg_vo['product']['remark'];
            $total_amount += $dg_vo['total_amount'] = $dg_vo['price']*$dg_vo['stock'];
            unset($dg_vo['product']['source']);
        }
        $vo['total_stock'] = $total_stock;
        $vo['total_amount'] = $total_amount;
        $data['product'] = $vo;
        $data['list'] = $dg_list;
        $this->ajaxSucReturn($data);
    }

    //仓库商品列表
    function productList(){
        $model = M(CONTROLLER_NAME);
        //$wdata['status'] = 1;
        //$kw_list = $model->where($wdata)->select();
        //$this->assign('kw_list',$kw_list);
        if(I('id')){
            $wdata['id'] = I('id');
            $ck_vo = $model->where($wdata)->find();
        }else{
            $_GET['id'] = $wdata['id'] = $kw_list[0]['id'];
            $ck_vo = $model->where($wdata)->find();
        }
        //$this->assign('ck_vo',$ck_vo);
        $p_list = $this->getProduct($ck_vo['atype']);
        //$this->assign('p_list',$p_list);
        $data['ck_vo'] = $ck_vo;
        $data['p_list'] = $p_list;
        $this->ajaxSucReturn($data);
    }

    //其他仓库调拨
    function transfer(){
        $request = I('');
        $kw_model = D('UserKuerpWarehouse');
        $kwlp_model = D('UserKuerpWarehouseLocProduct');
        if(I('item_id') && I('atype')){
            $kwlp_wdata['atype'] = I('atype');
            $kwlp_wdata['item_id'] = I('item_id');
            $p_list = $kwlp_model->where($kwlp_wdata)->select();
            //仓库
            $wh_wdata['atype'] = I('atype');
            $wh_wdata['status'] = 1;
            $kw_list = $kw_model->where($wh_wdata)->select();
        }else if(I('product_id') && I('atype')){
            $kwlp_wdata['atype'] = I('atype');
            $kwlp_wdata['product_id'] = I('product_id');
            $p_list = $kwlp_model->where($kwlp_wdata)->select();
            //仓库
            $wh_wdata['atype'] = I('atype');
            $wh_wdata['status'] = 1;
            $kw_list = $kw_model->where($wh_wdata)->select();
        }else if(I('wh_id')){
            $kw_wdata['id'] = I('wh_id');
            $kw_vo = $kw_model->where($kw_wdata)->find();
            $kwlp_wdata['wh_id'] = I('wh_id');
            $p_list = $kwlp_model->where($kwlp_wdata)->select();
            //仓库
            $wh_wdata['atype'] = $kw_vo['atype'];
            $wh_wdata['status'] = 1;
            $kw_list = $kw_model->where($wh_wdata)->select();
        }else if(I('atype')){
            $kwlp_wdata['atype'] = I('atype');
            $p_list = $kwlp_model->where($kwlp_wdata)->select();
            //仓库
            $wh_wdata['atype'] = I('atype');
            $wh_wdata['status'] = 1;
            $wh_wdata['is_delete'] = 0;
            $kw_list = $kw_model->where($wh_wdata)->select();
        }
        foreach($p_list as &$val){
            $val['guige'] = $val['product']['guige'];
            $val['color_name'] = $val['product']['color'];
            $val['unit'] = $val['product']['unit'];
        
        }
        $data['p_list'] = $p_list;
        $data['kw_list'] = $kw_list;
        $this->ajaxSucReturn($data);
    }

    //其他仓库调拨
    function transferSubmit(){
        $request = I('');
        $kw_model = D('UserKuerpWarehouse');
        $kwlp_model = D('UserKuerpWarehouseLocProduct');
        //dump($request);exit;
        $kw_model->startTrans();//启用事务
        foreach($request['nums'] as $key=>$val){
            $input['nums'] = $request['nums'][$key];
            if(!$input['nums']){
                continue;
            }
            $kwlp_id = $request['kwlp_ids'][$key];
            $kwlp_wdata['id'] = $kwlp_id;
            $kwlp_vo = $kwlp_model->where($kwlp_wdata)->find();
            if($input['nums']>$kwlp_vo['stock']){
                continue;
            }
            //dump($kwlp_vo);
            if($kwlp_vo['wh_id']==$request['wh_id'][$key] && $kwlp_vo['loc_id']==$request['loc_id'][$key]){
                $this->error('不能自己调拨到自己库位-'.$kwlp_vo['wh_name'].'-'.$kwlp_vo['loc_name']);
            }
            $input['kwlp_id'] = $kwlp_id;
            $input['resource_type'] = 'transfer';
            $input['wh_id'] = $request['wh_id'][$key];
            $input['loc_id'] = $request['loc_id'][$key];
            $input['batch_no'] = $request['batch_no'][$key];
            $input['nums'] = $request['nums'][$key];
            $input['remark'] = $request['remark'][$key];
            //dump($input);exit;
            $result = $kw_model->transfer($input);
            //dump($result);
            if(!$result['status']){
                //$this->ajaxErrReturn($result['msg']);
            }
        }
        //dump($request);exit;
        $kw_model->commit();//提交事务
        $this->ajaxSucReturn('调拨成功');
    }

    
}

?>